<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="取消"
  >
    <LabItemsBasisItemsForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false"></LabItemsBasisItemsForm>
    <LabItemsBasisItemsA ref="registerTable" :mainBasisId="mainBasisId"></LabItemsBasisItemsA>
    <template v-slot:footer>
      <a-button type="default" @click="handleCancel"> 取消 </a-button>
      <a-button type="primary" :loading="loading" @click="handleOk">确认</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineExpose, reactive } from 'vue';
  import LabItemsBasisItemsForm from './LabItemsBasisItemsForm.vue';
  import LabItemsBasisItemsA from './LabItemsBasisItemsA.vue';
  import { saveTargetList, getTargetList } from '../LabItemsBasis.api';

  const title = ref<string>('');
  const width = ref<number>(1100);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const registerForm = ref();
  const registerTable = ref();
  const emit = defineEmits(['success']);
  let mainBasisId = ref('');
  let loading = ref(false);
  /**
   * 新增
   */
  function add() {
    title.value = '新增';
    visible.value = true;
    nextTick(() => {
      registerForm.value.add();
    });
  }

  /**
   * 编辑
   * @param record
   */
  function edit(record) {
    console.log('record', record);
    mainBasisId.value = record.id;
    // if(registerTable.value.normList){
    //   registerTable.value.normList = [];
    // }
    title.value = disableSubmit.value ? '详情' : '编辑';
    visible.value = true;   
    nextTick(() => {
      registerTable.value.getTargetListData();
      registerForm.value.edit(record);
    });
  }  

  /**
   * 确定按钮点击事件
   */
  async function handleOk() {
    loading.value = true; // registerForm.value.submitForm();
    const labItemsBasisItemsVoPage = reactive({
      id: '',
      name: '',
      atta: '',
      labItemsBasisItemsVoList: [],
    });
    labItemsBasisItemsVoPage.labItemsBasisItemsVoList = registerTable.value.normList.map((item) => item);
    console.log('params', labItemsBasisItemsVoPage);
    const result = await saveTargetList(labItemsBasisItemsVoPage);
    loading.value = false;
    submitCallback();
    console.log('result', result);    
    // if (result.code === 200) {
     
    // }
  }

  /**
   * form保存回调事件
   */
  function submitCallback() {
    handleCancel();
    emit('success');
  }

  /**
   * 取消按钮回调事件
   */
  function handleCancel() {
    visible.value = false;
  }

  defineExpose({
    add,
    edit,
    disableSubmit,
  });
</script>

<style>
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }
</style>
